import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:love_app/common/slide_transition.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/model/invite_info_model.dart';
import 'package:love_app/providers/home_page_provider.dart';
import 'package:love_app/pages/home_page/first_page.dart';
import 'package:love_app/pages/home_page/more_page.dart';
import 'package:love_app/providers/share_provider.dart';
import 'package:love_app/service/user_info_api.dart';
import 'package:provider/provider.dart';

/// 底部导航栏首页内容
class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<HomePageProvider>(
      create: (_) => HomePageProvider(),
      child: PageContent(),
    );
  }
}

// 页面内容
class PageContent extends StatelessWidget {
  final Widget _firstPage = FirstPage(); // 第一个页面
  final Widget _secordPage = MorePage(); // 第二个页面

  @override
  Widget build(BuildContext context) {
    ShareProvider shareProvider =
        Provider.of<ShareProvider>(context, listen: false);
    _listenCopy(context, shareProvider);
    return Consumer<HomePageProvider>(
      builder: (context, HomePageProvider homePageModel, child) =>
          AnimatedSwitcher(
        // 这是一个新老widget交替
        duration: const Duration(milliseconds: 600),
        transitionBuilder: (Widget child, Animation<double> animation) {
          return SlideTransitionX(
            child: child,
            position: animation,
            direction: homePageModel.direction,
          );
        },
        // 要显示的页面
        child: homePageModel.isMorePage ? _secordPage : _firstPage,
      ),
    );
  }

  // 监听剪切板
  Future<void> _listenCopy(
      BuildContext context, ShareProvider shareProvider) async {
    // 只有第一次进来主页才会显示弹出邀请框
    if (shareProvider.copyData == false) {
      shareProvider.copyData = true;
      Future.delayed(Duration(seconds: 5), () async {
        try {
          var text = await Clipboard.getData(Clipboard.kTextPlain);
          RegExp re = new RegExp(r"[A-Za-z0-9]{19}");
          // 通过正则表达式获取剪切板的邀请码
          var code = re.allMatches(text.text).first.group(0);
          InviteInfoModel inviteInfoModel = await UserInfoApi().invitationUserInfo(shareProvider.token, code);
          if(inviteInfoModel.code == 200){
            await _show(context, inviteInfoModel); 
          }
        } catch (error) {
          // Su.suPrints("获取邀请码异常");
        }
      });
    }
  }

  Future<Widget> _show(BuildContext context, InviteInfoModel inviteInfoModel) {
    return showDialog(
      context: context,
      builder: (BuildContext content) {
        return _Invitations(inviteInfoModel: inviteInfoModel);
      },
    );
  }
}

// 邀请人信息弹窗
class _Invitations extends StatelessWidget {

  final InviteInfoModel inviteInfoModel;

  const _Invitations({Key key, this.inviteInfoModel}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _cententWidget(context),
    );
  }

  Widget _cententWidget(BuildContext context) {
    return AlertDialog(
      contentPadding: EdgeInsets.all(0),
      shape: RoundedRectangleBorder(
          // 边框设置
          borderRadius: BorderRadius.all(Radius.circular(20))),
      content: Container(
        width: Su().w(589),
        height: Su().h(658),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            _headerImage(),
            _userName(),
            _agreeButton(),
            _line(),
            _refuseText(),
            _refuseButton(context),
          ],
        ),
      ),
    );
  }

  // 头像
  Widget _headerImage() {
    Su.suPrints(inviteInfoModel.data.headimgurl, msg: "打印头像");
    return Padding(
      padding: EdgeInsets.only(top: Su().w(50)),
      child: ClipOval(
        child: Image.network(
          inviteInfoModel.data.headimgurl,
          width: Su().w(137.8),
          height: Su().w(137.8),
        ),
      ),
    );
  }

  // 用户昵称
  Widget _userName() {
    String name = inviteInfoModel.data.nickname;
    String sex = inviteInfoModel.data.sex;
    String text = "(用户$name·$sex) 邀请你成为情侣";
    return Padding(
      padding: EdgeInsets.only(
        top: Su().h(27),
      ),
      child: Text(
        text,
        style: Su().textStyle(Color.fromRGBO(38, 38, 38, 1), 23.52),
      ),
    );
  }

  // 同意按钮
  Widget _agreeButton() {
    return InkWell(
      onTap: () {},
      child: Container(
        width: Su().w(475.8),
        height: Su().h(75.6),
        alignment: Alignment.center,
        margin: EdgeInsets.only(
          top: Su().h(26),
        ),
        decoration: BoxDecoration(
          gradient: LinearGradient(colors: [
            Color.fromRGBO(238, 127, 168, 1),
            Color.fromRGBO(237, 120, 127, 1),
          ]),
          borderRadius: BorderRadius.circular(Su().w(20)),
        ),
        child: Text(
          "同意邀请",
          style: Su().textStyle(Colors.white, 28.56),
        ),
      ),
    );
  }

  // 一条线
  Widget _line() {
    return Container(
      color: Color.fromRGBO(125, 125, 125, 1),
      padding: EdgeInsets.only(
        top: Su().h(60),
      ),
      width: Su().w(500),
      height: Su().h(1),
    );
  }

  // 拒绝邀请文字
  Widget _refuseText() {
    return Container(
      padding: EdgeInsets.only(
        top: Su().h(110),
      ),
      child: Text(
        "拒绝邀请",
        style: Su().textStyle(Color.fromRGBO(178, 178, 178, 1), 23.52),
      ),
    );
  }

  // 同意按钮
  Widget _refuseButton(BuildContext context) {
    return InkWell(
      onTap: () {
        Navigator.pop(context);
      },
      child: Container(
        width: Su().w(475.8),
        height: Su().h(75.6),
        alignment: Alignment.center,
        margin: EdgeInsets.only(
          top: Su().h(26),
        ),
        decoration: BoxDecoration(
          color: Color.fromRGBO(236, 236, 237, 1),
          borderRadius: BorderRadius.circular(Su().w(20)),
        ),
        child: Text(
          "拒绝邀请",
          style: Su().textStyle(Color.fromRGBO(236, 100, 126, 1), 28.56),
        ),
      ),
    );
  }
}

// // 邀请弹窗
// class _Invitation extends StatefulWidget {
//   final String code; // 邀请码

//   const _Invitation({Key key, this.code}) : super(key: key);

//   @override
//   __InvitationState createState() => __InvitationState();
// }

// class __InvitationState extends State<_Invitation> {
//   ShareProvider _shareProvider;
//   InviteInfoModel _inviteInfoModel;

//   @override
//   void didChangeDependencies() {
//     super.didChangeDependencies();
//     _shareProvider = Provider.of<ShareProvider>(context, listen: false);
//   }

//   @override
//   Widget build(BuildContext context) {
//     return FutureBuilder(
//       future: _getCodeInfo(),
//       builder: (BuildContext context, AsyncSnapshot snapshot) {
//         if (snapshot.connectionState == ConnectionState.done) {
//           _inviteInfoModel = snapshot.data;
//           return _cententWidget();
//         }
//         return Container();
//       },
//     );
//   }

//   // 通过code获取信息
//   Future<InviteInfoModel> _getCodeInfo() async {
//     return await UserInfoApi()
//         .invitationUserInfo(_shareProvider.token, widget.code);
//   }

//   Widget _cententWidget() {
//     return AlertDialog(
//       contentPadding: EdgeInsets.all(0),
//       shape: RoundedRectangleBorder(
//           // 边框设置
//           borderRadius: BorderRadius.all(Radius.circular(20))),
//       content: Container(
//         width: Su().w(589),
//         height: Su().h(658),
//         child: Column(
//           mainAxisAlignment: MainAxisAlignment.start,
//           crossAxisAlignment: CrossAxisAlignment.center,
//           children: <Widget>[
//             _headerImage(),
//             _userName(),
//             _agreeButton(),
//             _line(),
//             _refuseText(),
//             _refuseButton(),
//           ],
//         ),
//       ),
//     );
//   }

//   // 头像
//   Widget _headerImage() {
//     Su.suPrints(_inviteInfoModel.data.headimgurl, msg: "打印头像");
//     return Padding(
//       padding: EdgeInsets.only(top: Su().w(50)),
//       child: ClipOval(
//         child: Image.network(
//           _inviteInfoModel.data.headimgurl,
//           width: Su().w(137.8),
//           height: Su().w(137.8),
//         ),
//       ),
//     );
//   }

//   // 用户昵称
//   Widget _userName() {
//     String name = _inviteInfoModel.data.nickname;
//     String sex = _inviteInfoModel.data.sex;
//     String text = "(用户$name·$sex) 邀请你成为情侣";
//     return Padding(
//       padding: EdgeInsets.only(
//         top: Su().h(27),
//       ),
//       child: Text(
//         text,
//         style: Su().textStyle(Color.fromRGBO(38, 38, 38, 1), 23.52),
//       ),
//     );
//   }

//   // 同意按钮
//   Widget _agreeButton() {
//     return InkWell(
//       onTap: () {},
//       child: Container(
//         width: Su().w(475.8),
//         height: Su().h(75.6),
//         alignment: Alignment.center,
//         margin: EdgeInsets.only(
//           top: Su().h(26),
//         ),
//         decoration: BoxDecoration(
//           gradient: LinearGradient(colors: [
//             Color.fromRGBO(238, 127, 168, 1),
//             Color.fromRGBO(237, 120, 127, 1),
//           ]),
//           borderRadius: BorderRadius.circular(Su().w(20)),
//         ),
//         child: Text(
//           "同意邀请",
//           style: Su().textStyle(Colors.white, 28.56),
//         ),
//       ),
//     );
//   }

//   // 一条线
//   Widget _line() {
//     return Container(
//       color: Color.fromRGBO(125, 125, 125, 1),
//       padding: EdgeInsets.only(
//         top: Su().h(60),
//       ),
//       width: Su().w(500),
//       height: Su().h(1),
//     );
//   }

//   // 拒绝邀请文字
//   Widget _refuseText() {
//     return Container(
//       padding: EdgeInsets.only(
//         top: Su().h(110),
//       ),
//       child: Text(
//         "拒绝邀请",
//         style: Su().textStyle(Color.fromRGBO(178, 178, 178, 1), 23.52),
//       ),
//     );
//   }

//   // 同意按钮
//   Widget _refuseButton() {
//     return InkWell(
//       onTap: () {
//         Navigator.pop(context);
//       },
//       child: Container(
//         width: Su().w(475.8),
//         height: Su().h(75.6),
//         alignment: Alignment.center,
//         margin: EdgeInsets.only(
//           top: Su().h(26),
//         ),
//         decoration: BoxDecoration(
//           color: Color.fromRGBO(236, 236, 237, 1),
//           borderRadius: BorderRadius.circular(Su().w(20)),
//         ),
//         child: Text(
//           "拒绝邀请",
//           style: Su().textStyle(Color.fromRGBO(236, 100, 126, 1), 28.56),
//         ),
//       ),
//     );
//   }
// }
